{% extends 'base.html' %}
{% load static %}

{% block title %}{{ page_title }}{% endblock %}

{% block content %}
<div class="container py-4">
    <!-- 顶部横幅 -->
    <div class="row mb-4">
        <div class="col-12">
            <div class="card bg-primary text-white">
                <div class="card-body py-4">
                    <div class="row align-items-center">
                        <div class="col-md-8">
                            <h2 class="mb-2">🎁 积分商城</h2>
                            <p class="mb-0">用积分兑换心仪好礼，让每一分都有价值</p>
                        </div>
                        <div class="col-md-4 text-end">
                            <div class="d-inline-block bg-white text-primary rounded-pill px-4 py-2">
                                <i class="fas fa-coins me-2"></i>
                                <span class="fw-bold fs-5">{{ user.points }}</span>
                                <small class="text-muted">当前积分</small>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- 可用商品 -->
    <div class="row mb-5">
        <div class="col-12">
            <h4 class="mb-3">🛍️ 可用商品</h4>
            {% if available_rewards %}
            <div class="row g-3">
                {% for reward in available_rewards %}
                <div class="col-xl-3 col-lg-4 col-md-6">
                    <div class="card reward-card h-100 shadow-sm">
                        <div class="position-relative">
                            {% if reward.image %}
                            <img src="{{ reward.image.url }}" class="card-img-top" alt="{{ reward.name }}" 
                                 style="height: 200px; object-fit: cover;">
                            {% else %}
                            <div class="card-img-top bg-light d-flex align-items-center justify-content-center" 
                                 style="height: 200px;">
                                <i class="fas fa-gift fa-3x text-muted"></i>
                            </div>
                            {% endif %}
                            {% if reward.reward_type == 'coupon' %}
                            <span class="position-absolute top-0 end-0 badge bg-warning m-2">优惠券</span>
                            {% elif reward.reward_type == 'privilege' %}
                            <span class="position-absolute top-0 end-0 badge bg-info m-2">特权</span>
                            {% elif reward.reward_type == 'physical' %}
                            <span class="position-absolute top-0 end-0 badge bg-success m-2">实物</span>
                            {% endif %}
                        </div>
                        
                        <div class="card-body d-flex flex-column">
                            <h6 class="card-title">{{ reward.name }}</h6>
                            <p class="card-text text-muted small flex-grow-1">{{ reward.description|truncatechars:60 }}</p>
                            
                            <div class="d-flex justify-content-between align-items-center mt-auto">
                                <div class="text-primary fw-bold">
                                    <i class="fas fa-coins me-1"></i>{{ reward.points_required }}
                                </div>
                                <small class="text-muted">
                                    库存: {{ reward.stock_quantity }}
                                </small>
                            </div>
                            
                            <div class="mt-2">
                                <a href="{% url 'location:reward_detail' reward.id %}" class="btn btn-primary btn-sm w-100">
                                    立即兑换
                                </a>
                            </div>
                        </div>
                    </div>
                </div>
                {% endfor %}
            </div>
            {% else %}
            <div class="text-center py-5">
                <i class="fas fa-gift fa-3x text-muted mb-3"></i>
                <h5 class="text-muted">暂无可用商品</h5>
                <p class="text-muted">所有商品都已售罄，敬请期待上新</p>
            </div>
            {% endif %}
        </div>
    </div>

    <!-- 售罄商品 -->
    {% if sold_out_rewards %}
    <div class="row">
        <div class="col-12">
            <h4 class="mb-3">⏳ 已售罄</h4>
            <div class="row g-3">
                {% for reward in sold_out_rewards %}
                <div class="col-xl-3 col-lg-4 col-md-6">
                    <div class="card reward-card h-100 shadow-sm opacity-75">
                        <div class="position-relative">
                            {% if reward.image %}
                            <img src="{{ reward.image.url }}" class="card-img-top" alt="{{ reward.name }}" 
                                 style="height: 200px; object-fit: cover; filter: grayscale(50%);">
                            {% else %}
                            <div class="card-img-top bg-light d-flex align-items-center justify-content-center" 
                                 style="height: 200px;">
                                <i class="fas fa-gift fa-3x text-muted"></i>
                            </div>
                            {% endif %}
                            <span class="position-absolute top-0 end-0 badge bg-secondary m-2">已售罄</span>
                        </div>
                        
                        <div class="card-body d-flex flex-column">
                            <h6 class="card-title text-muted">{{ reward.name }}</h6>
                            <p class="card-text text-muted small flex-grow-1">{{ reward.description|truncatechars:60 }}</p>
                            
                            <div class="d-flex justify-content-between align-items-center mt-auto">
                                <div class="text-muted fw-bold">
                                    <i class="fas fa-coins me-1"></i>{{ reward.points_required }}
                                </div>
                                <small class="text-muted">
                                    库存: {{ reward.stock_quantity }}
                                </small>
                            </div>
                            
                            <div class="mt-2">
                                <button class="btn btn-secondary btn-sm w-100" disabled>
                                    已售罄
                                </button>
                            </div>
                        </div>
                    </div>
                </div>
                {% endfor %}
            </div>
        </div>
    </div>
    {% endif %}
</div>
{% endblock %}